package com.dashunzi.yueyutianqi.pager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dashunzi.yueyutianqi.Bean.TrainBean;
import com.dashunzi.yueyutianqi.R;
import com.dashunzi.yueyutianqi.base.BasePager;
import com.dashunzi.yueyutianqi.util.HttpCallbackListener;
import com.dashunzi.yueyutianqi.util.HttpUtil;
import com.google.gson.Gson;


/**
 * Created by Administrator on 2016/3/10 0010.
 * 作者：大顺子
 */
public class trainPager extends BasePager {

    public static final String TRAIN_QUEUE = "http://op.juhe.cn/onebox/train/query?key=a11e91ae584fc44662e35de3717f4da5&train=";
    private Button switchCity;
    private TextView cityName;
    private Button refreshWeather;
    private EditText etTrain;//用户输入的车次
    private TextView startStation;//起始站
    private TextView startTime;//起始时间
    private TextView endStation;//终点站
    private TextView endTime;//到达时间
    private TextView priceType1;//类型1
    private TextView price1;//价格1
    private TextView priceType2;//类型2
    private TextView price2;//价格2
    private TextView trainType;//火车类型
    private Button mBtnQueue;//点击查询
    private String mTrain;
    private TrainBean mTrainBean;
    private int mError_code;

    private void assignViews() {

    }

    private View mView;
    private Activity trainActivity;

    public trainPager(Activity activity) {
        super(activity);
        trainActivity = activity;
    }

    /**
     * 初始化view
     */
    @Override
    public void initview() {
        mView = View.inflate(mActivity, R.layout.train_layout, null);
        switchCity = (Button) mView.findViewById(R.id.switch_city);
        cityName = (TextView) mView.findViewById(R.id.city_name);
        refreshWeather = (Button) mView.findViewById(R.id.refresh_weather);
        etTrain = (EditText) mView.findViewById(R.id.et_train);
        startStation = (TextView) mView.findViewById(R.id.start_station);
        startTime = (TextView) mView.findViewById(R.id.start_time);
        endStation = (TextView) mView.findViewById(R.id.end_station);
        endTime = (TextView) mView.findViewById(R.id.end_time);
        priceType1 = (TextView) mView.findViewById(R.id.price_type1);
        price1 = (TextView) mView.findViewById(R.id.price1);
        priceType2 = (TextView) mView.findViewById(R.id.price_type2);
        price2 = (TextView) mView.findViewById(R.id.price2);
        trainType = (TextView) mView.findViewById(R.id.train_type);
        mBtnQueue = (Button) mView.findViewById(R.id.btn_queue);
    }

    @Override
    public void initData() {
        flContent.removeAllViews();


        //向FrameLayout中动态添加布局
        flContent.addView(mView);
    }

    @Override
    public void initListener() {
        mBtnQueue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTrain = etTrain.getText().toString();
                String address = TRAIN_QUEUE + mTrain;//拼接字符串
                HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
                    @Override
                    public void onFinish(String response) {
                        System.out.println("response---------" + response);
                        pareseData(response);
                        //System.out.println("--" + mTrainBean.getResult().getList().getEnd_station());

                        mError_code = mTrainBean.getError_code();

                        mActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mError_code == 0) {
                                    // System.out.println(error_code);
                                    startStation.setText(mTrainBean.getResult().getList().getStart_station());//设置始发站
                                    startTime.setText(mTrainBean.getResult().getList().getStart_time()); //设置始发时间
                                    endStation.setText(mTrainBean.getResult().getList().getEnd_station());//设置终点站
                                    endTime.setText(mTrainBean.getResult().getList().getEnd_time());//设置到达时间
                                    priceType1.setText(mTrainBean.getResult().getList().getPrice_list().getItem().get(0).getPrice_type());//类型1
                                    price1.setText(mTrainBean.getResult().getList().getPrice_list().getItem().get(0).getPrice());////价格1
                                    priceType2.setText(mTrainBean.getResult().getList().getPrice_list().getItem().get(1).getPrice_type());//类型2
                                    price2.setText(mTrainBean.getResult().getList().getPrice_list().getItem().get(1).getPrice());//价格2
                                    trainType.setText(mTrainBean.getResult().getList().getTrain_type());//火车类型
                                } else {
                                    new AlertDialog.Builder(trainActivity).setTitle("查询不到火车该班次相关信息,请输入正确的火车车次!").show();
                                }
                            }
                        });


                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });


            }
        });
    }

    public void pareseData(String string) {
        Gson gson = new Gson();
        mTrainBean = new TrainBean();
        mTrainBean = gson.fromJson(string, TrainBean.class);
    }
}
